package net.comm.page;

import java.util.*;

/**
 * @author mao
 * 
 */

public class PageCtr {

	private int curPage = 0; // 当前页数

	private int pos = 0;	//当前记录位置

	private int totalNum = 0;// 总记录数

	private int pageSize = 10; // 每页10条

	private int totalPages = 0; // 总的页数

	private int residualcount=0;  //剩余记录数

	private String direction = "N";

    private String strSql = "";

    private String[] keyNameArray=null;  //key value

    private HashMap orderByValueMap=null;  //key value  obj(datatype,value)

    private String errmsg="";

    private boolean hasPrevious;  // 是否有上一页

    private boolean hasNext;  	  // 是否有下一页


	public PageCtr(int currentPage,int pageSize,String direction,String strSql) 
	{
		this.curPage = currentPage;
		this.pageSize = pageSize;
		this.direction=direction;
		this.strSql = strSql;
	}

	public void caculate()
	{
		if(totalNum%pageSize==0){
		   totalPages=totalNum/pageSize;
	    }else{
	        totalPages=totalNum/pageSize+1;
        }
		
		if(totalPages > 0 && curPage > totalPages) {
			curPage = totalPages;
	    }
		
		pos = (curPage - 1) * pageSize + 1;
		
		/**
		 * 如果标志为L,但是totalPages不等于curPage
		 * 防止点击尾页,然后用户点击添加,引起的当前页显示错误问题
		 */
		if("L".equals(direction) && curPage < totalPages)
		{
			curPage = totalPages;
		}
		
		/**
		 * 如果当前页等于最后一页,把direction标志改成L
		 * 防止删除一条后，总页数少了一页,然后用户点击添加添加成功后返回造成的selectByPage错误
		 * 不过这里还会产生一个问题，就是用户添加成功后会跳到最后一页
		 */
		if(curPage == totalPages) {
			direction = "L";
		}
		
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public String[] getKeyNameArray() {
		return keyNameArray;
	}

	public void setKeyNameArray(String[] keyNameArray) {
		this.keyNameArray = keyNameArray;
	}

	public HashMap getOrderByValueMap() {
		return orderByValueMap;
	}

	public void setOrderByValueMap(HashMap orderByValueMap) {
		this.orderByValueMap = orderByValueMap;
	}

	/**
	 * @return the pos
	 */
	public int getPos() {
		return pos;
	}

	/**
	 * @param pos the pos to set
	 */
	public void setPos(int pos) {
		this.pos = pos;
	}

	public int getPrepage() 
	{
		if (curPage - 1 >= 0) {
			return curPage - 1;
		} else {
			return 0;
		}
	}

	public int getNextpage() 
	{
		if (curPage + 1 <= totalPages) {
			return curPage + 1;
		} else {
			return totalPages;
		}
	}

	public void setNextAndLast()
	{
		if(curPage==totalPages&&curPage>1)
	    {
	        hasNext=false;
	        hasPrevious=true;
	    }
	    else if(curPage==totalPages&&totalPages==1)
	    {
	        hasNext=false;
	        hasPrevious=false;
	    }
	    else if(curPage<totalPages&&curPage>1)
	    {
	        hasNext=true;
	        hasPrevious=true;
	    }
	    else if(curPage<totalPages&&curPage==1)
	    {
	        hasNext=true;
	        hasPrevious=false;
	    }

	}

	public synchronized int getResidualcount() {
		return residualcount;
	}

	public synchronized void setResidualcount(int residualcount) {
		this.residualcount = residualcount;
	}

}

